<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-upload2" size="mini" @click="handleImport">上传
        </el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" type="index" align="center"/>
      <el-table-column label="ID" align="center" prop="pid"/>
      <el-table-column label="排序优先级" align="center" prop="sort_id"></el-table-column>
      <el-table-column label="位置" align="center" prop="location_name"/>
      <el-table-column label="公司" align="center" prop="companyname"/>
      <el-table-column label="图片" align="center" prop="img_url">
        <template #default="{row}">
          <el-image :src="api+row.img_url"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改选科分数对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="400px" append-to-body>
      <image-upload-form ref="form" :form-options="form" :location-type="locationType"></image-upload-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 用户导入对话框 -->
    <el-dialog v-if="upload.open" :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <image-upload ref="upload" :form-options="form" :location-type="locationType" @success="handleUploadSuccess"></image-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {DeleteImg, GetImgSortList, SortImg, GetMaxSortId} from "@/api/upload"
import ImageUpload from "@/components/ImageUpload/index";
import ImageUploadForm from "@/components/ImageUpload/form";

const defaultSettings = require('@/settings.js')

export default {
  name: "upload-index",
  components: {ImageUploadForm, ImageUpload},
  data() {
    return {
      uploadingImg: '',
      api: defaultSettings.server,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 选科分数表格数据
      dataList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: '',
        // 导入名称
        fileName: '',
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        // headers: {Authorization: 'Bearer ' + auth.getToken()},
        // 上传的地址
        url: '/api/User/UploadFile'
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        scoreName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        scoreName: [{
          required: true,
          message: '导入名称必填'
        }]
      }
    }
  },
  computed: {
    locationType() {
      return this.$route.meta.locationType
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询选科分数列表 */
    getList() {
      this.loading = true
      GetImgSortList(this.locationType).then(response => {
        this.dataList = response['result']['img_list'] || []
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {

    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.importId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    async handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.form = this.deepClone(row)
      this.open = true
      this.title = '修改图片信息'
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate().then(() => {
        SortImg(this.form).then(_ => {
          this.msgSuccess('修改成功')
          this.open = false
          this.getList()
        })
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.pid || this.ids
      this.$confirm('是否确认删除ID为"' + ids + '"的图片?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return DeleteImg(this.locationType, ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    async handleImport() {
      const res = await GetMaxSortId()
      this.form = {
        sort_id: res.result.maxSortId + 1,
        companyname: ''
      }
      this.upload.open = true
      this.upload.title = '上传图片'
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit()
    },
    handleUploadSuccess() {
      this.upload.open = false
      this.handleQuery()
    }
  }
}
</script>

<style scoped>

</style>
